package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import model.Behandling;
import service.Service;

/**
 * 
 * @author Søren
 * 
 */
public class CRUBehandlingDialog extends JDialog {
	private JLabel lblBehandlingNavn;
	private JTextField txfBehandlingNavn;
	private JRadioButton rdbtnOpretTrretid;
	private JRadioButton rdbtnOpretDragering;
	private JLabel lblMaksimalTrretidNr;
	private JTextField txfMaxPakkeklar;
	private JLabel lblMinimumTrretid;
	private JLabel lblIdeelTrretid;
	private JLabel lblMaksimalTrretid;
	private JTextField txfMin;
	private JTextField txfIdeel;
	private JTextField txfMax;
	private JButton btnFrdig;
	private JButton btnTilfj;
	private JScrollPane scrollPane;
	private JList list;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private Behandling behandling;
	private JButton btnFjern;

	public CRUBehandlingDialog(JFrame owner, Behandling behandling) {
		super(owner);
		setResizable(false);
		getContentPane().setLayout(null);
		setSize(455, 435);
		setModalityType(ModalityType.APPLICATION_MODAL);

		lblBehandlingNavn = new JLabel("Navn:");
		lblBehandlingNavn.setFont(new Font("Dialog", Font.BOLD, 20));
		lblBehandlingNavn.setBounds(12, 39, 79, 34);
		getContentPane().add(lblBehandlingNavn);

		txfBehandlingNavn = new JTextField();
		txfBehandlingNavn.setBounds(297, 42, 141, 34);
		getContentPane().add(txfBehandlingNavn);
		txfBehandlingNavn.setColumns(10);

		rdbtnOpretTrretid = new JRadioButton("Opret tørretid");
		rdbtnOpretTrretid
				.addActionListener(new RdbtnOpretTrretidActionListener());
		buttonGroup.add(rdbtnOpretTrretid);
		rdbtnOpretTrretid.setSelected(false);
		rdbtnOpretTrretid.setFont(new Font("Dialog", Font.BOLD, 18));
		rdbtnOpretTrretid.setBounds(12, 147, 178, 23);
		getContentPane().add(rdbtnOpretTrretid);

		rdbtnOpretDragering = new JRadioButton("Opret Dragering");
		rdbtnOpretDragering
				.addActionListener(new RdbtnOpretDrageringActionListener());
		buttonGroup.add(rdbtnOpretDragering);
		rdbtnOpretDragering.setFont(new Font("Dialog", Font.BOLD, 18));
		rdbtnOpretDragering.setBounds(207, 147, 191, 23);
		getContentPane().add(rdbtnOpretDragering);

		lblMaksimalTrretidNr = new JLabel("Max pakkeklar ventetid:");
		lblMaksimalTrretidNr.setFont(new Font("Dialog", Font.BOLD, 20));
		lblMaksimalTrretidNr.setBounds(12, 88, 386, 34);
		getContentPane().add(lblMaksimalTrretidNr);

		txfMaxPakkeklar = new JTextField();
		txfMaxPakkeklar.setColumns(10);
		txfMaxPakkeklar.setBounds(297, 91, 141, 34);
		getContentPane().add(txfMaxPakkeklar);

		lblMinimumTrretid = new JLabel("Minimum tørretid:");
		lblMinimumTrretid.setFont(new Font("Dialog", Font.BOLD, 18));
		lblMinimumTrretid.setBounds(12, 187, 241, 15);
		getContentPane().add(lblMinimumTrretid);

		lblIdeelTrretid = new JLabel("Ideel tørretid:");
		lblIdeelTrretid.setFont(new Font("Dialog", Font.BOLD, 18));
		lblIdeelTrretid.setBounds(12, 227, 241, 15);
		getContentPane().add(lblIdeelTrretid);

		lblMaksimalTrretid = new JLabel("Maksimal tørretid:");
		lblMaksimalTrretid.setFont(new Font("Dialog", Font.BOLD, 18));
		lblMaksimalTrretid.setBounds(12, 268, 241, 15);
		getContentPane().add(lblMaksimalTrretid);

		txfMin = new JTextField();
		txfMin.setColumns(10);
		txfMin.setBounds(297, 178, 141, 34);
		getContentPane().add(txfMin);

		txfIdeel = new JTextField();
		txfIdeel.setColumns(10);
		txfIdeel.setBounds(297, 218, 141, 34);
		getContentPane().add(txfIdeel);

		txfMax = new JTextField();
		txfMax.setColumns(10);
		txfMax.setBounds(297, 259, 141, 34);
		getContentPane().add(txfMax);

		btnFrdig = new JButton("Færdig");
		btnFrdig.addActionListener(new BtnFrdigActionListener());
		btnFrdig.setBounds(297, 363, 141, 25);
		getContentPane().add(btnFrdig);

		btnTilfj = new JButton("Tilføj");
		btnTilfj.addActionListener(new BtnTilfjActionListener());
		btnTilfj.setBounds(297, 305, 141, 25);
		getContentPane().add(btnTilfj);

		scrollPane = new JScrollPane();
		scrollPane.setBounds(12, 295, 191, 109);
		getContentPane().add(scrollPane);

		list = new JList();
		scrollPane.setViewportView(list);

		btnFjern = new JButton("Fjern");
		btnFjern.addActionListener(new BtnFjernActionListener());
		btnFjern.setBounds(297, 335, 141, 23);
		getContentPane().add(btnFjern);
		rdbtnOpretTrretid.setSelected(true);

		if (behandling != null) {
			this.behandling = behandling;
			list.setListData(behandling.getDelBehandlinger().toArray());
			txfBehandlingNavn.setText(behandling.getNavn());
			txfMaxPakkeklar.setText(behandling.getDelBehandlinger()
					.get(behandling.getDelBehandlinger().size() - 1)
					.getMaxTid()
					+ "");
		}

	}

	public void updateList() {
		list.setListData(behandling.getDelBehandlinger().toArray());
	}

	private class BtnTilfjActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			if (behandling == null) {
				if (!txfBehandlingNavn.getText().equals("")
						&& !txfMaxPakkeklar.getText().equals("")) {
					String navn = txfBehandlingNavn.getText();
					try {
						int max = Integer.parseInt(txfMaxPakkeklar.getText());
						behandling = Service.opretBehandling(navn, max);
					} catch (Exception e) {
						txfMaxPakkeklar.setText("Skal være et heltal!");

					}

				}
				updateList();
			}
			if (behandling != null) {
				if (rdbtnOpretTrretid.isSelected()) {
					try {
						int min = Integer.parseInt(txfMin.getText());
						int ideel = Integer.parseInt(txfIdeel.getText());
						int max = Integer.parseInt(txfMax.getText());
						behandling.createDelBehandling(min, ideel, max);
					} catch (Exception e) {

					}
				} else if (rdbtnOpretDragering.isSelected()) {
					behandling.createDelBehandling();

				}
				updateList();
			}
		}
	}

	private class RdbtnOpretDrageringActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			txfMin.setEnabled(false);
			txfIdeel.setEnabled(false);
			txfMax.setEnabled(false);
		}
	}

	private class RdbtnOpretTrretidActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			txfMin.setEnabled(true);
			txfIdeel.setEnabled(true);
			txfMax.setEnabled(true);
		}
	}

	private class BtnFrdigActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			service.Service.updateBehandling(txfBehandlingNavn.getText(),
					behandling.getDelBehandlinger(), behandling);
			
			setVisible(false);
		}
	}

	private class BtnFjernActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			if (!list.isSelectionEmpty()) {
				if (list.getSelectedIndex() != behandling.getDelBehandlinger()
						.size() - 1)
					behandling.removeDelBehandling(behandling
							.getDelBehandlinger().get(list.getSelectedIndex()));
				updateList();
			}
		}
	}
}
